package com.oauth.service.application.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Required;

import com.oauth.service.application.beans.ApplicationServiceBean;
import com.oauth.service.application.beans.MemberApplicationRelation;
import com.oauth.service.application.dao.ApplicationDAO;

public class ApplicationServiceImpl implements ApplicationService {

	private ApplicationDAO mApplicationDAO;
	
	
	public ApplicationDAO getApplicationDAO() {
		return mApplicationDAO;
	}


	@Required
	public void setApplicationDAO(ApplicationDAO pApplicationDAO) {
		mApplicationDAO = pApplicationDAO;
	}


	public MemberApplicationRelation addMemberToApplication(Long pMemberId,
			Long pApplicationId) {
		
		return getApplicationDAO().addMemberToApplication(pMemberId, pApplicationId);
	}

	
	public ApplicationServiceBean addNewApplication(
			ApplicationServiceBean pApplicationServiceBean) {
		return getApplicationDAO().addNewApplication(pApplicationServiceBean);
	}

	
	public ApplicationServiceBean getApplicationById(Long pApplicationId) {
		return getApplicationDAO().getApplicationById(pApplicationId);
	}

	
	public List<ApplicationServiceBean> getMemberApplications(Long pMemberId) {
		 MemberApplicationRelation memberApplications = getApplicationDAO().getMemberApplications(pMemberId);
		 if(memberApplications != null){
			 return getApplicationDAO().getApplicationByIds(memberApplications.getApplicationIdSet());
		 }else{
			 return new ArrayList<ApplicationServiceBean>(1);
		 }
	}

	public List<ApplicationServiceBean>  getUnSubscribedApplications(Long pMemberId){
		List<Long> applicationIds = getApplicationDAO().getUnSubscribedApplications(pMemberId);
		List<ApplicationServiceBean> unSubscribedApplications = getApplicationDAO().getApplicationByIds(applicationIds);
		return unSubscribedApplications;
	}
	
	public MemberApplicationRelation removeMemberToApplication(Long pMemberId,
			Long pApplicationId) {
		return getApplicationDAO().removeMemberToApplication(pMemberId, pApplicationId);
	}

}
